package com.servlet.web;

import cn.hutool.crypto.SecureUtil;
import com.servlet.bean.User;
import com.servlet.service.UserService;
import com.servlet.service.impl.UserServiceImpl;
import com.servlet.utils.BaseServlet;
import com.servlet.utils.ProxyHelper;
import com.servlet.utils.ResponseVo;
import com.servlet.utils.ServletUtils;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Title: UserController
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2023/12/25 15:15
 */
@WebServlet("/user")
public class UserController extends BaseServlet {

    private UserService userService =(UserService) ProxyHelper.getProxyInstance(new UserServiceImpl());

    public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            String rememberme = req.getParameter("rememberme");
            boolean b = rememberme != null  && !"".equals(rememberme);
            password = SecureUtil.md5(password);
            User user = userService.loginUser(username);
            if(user == null) {
                ServletUtils.writeResponse(resp, ResponseVo.error("用户名不存在!"));
                return;
            }
            if(!user.getPassword().equals(password)) {
                ServletUtils.writeResponse(resp, ResponseVo.error("密码错误!"));
                return;
            }
            // 登录成功 将用户存入到session中并且返回
            HttpSession session = req.getSession();
            if( b) {
                Cookie cookie = new Cookie("JSESSIONID",session.getId());
                cookie.setMaxAge(60*60*24);
                resp.addCookie(cookie);
            }

            session.setAttribute("user",user);
            ServletUtils.writeResponse(resp, ResponseVo.ok("用户登录成功!",user));
        }catch (Exception e) {
            ServletUtils.writeResponse(resp, ResponseVo.error(e.getMessage()));
        }
    }

    /**
     * 注册
     * @param req
     * @param resp
     * @throws IOException
     */
    public void register(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            password = SecureUtil.md5(password);
            User user = new User(username,password);
            userService.registerUser(user);
            ServletUtils.writeResponse(resp, ResponseVo.ok("用户注册成功!",user));
        }catch (Exception e) {
            ServletUtils.writeResponse(resp, ResponseVo.error(e.getMessage()));
        }
    }

    public void current(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            HttpSession session = req.getSession();
            User user =(User) session.getAttribute("user");
            ServletUtils.writeResponse(resp, ResponseVo.ok("获取用户成功!",user));
        }catch (Exception e) {
            ServletUtils.writeResponse(resp, ResponseVo.error(e.getMessage()));
        }
    }

    public void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            HttpSession session = req.getSession();
            session.invalidate();
            ServletUtils.writeResponse(resp, ResponseVo.ok("退出成功!"));
        }catch (Exception e) {
            ServletUtils.writeResponse(resp, ResponseVo.error(e.getMessage()));
        }
    }

    public void online(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            ServletContext servletContext = req.getServletContext();
            List<User> list =(List<User>) servletContext.getAttribute("online");
            ServletUtils.writeResponse(resp, ResponseVo.ok("获取当前人数成功!",list));
        }catch (Exception e) {
            ServletUtils.writeResponse(resp, ResponseVo.error(e.getMessage()));
        }
    }
}
